<link rel="stylesheet" media="screen" href="/application/lib/datatables/css/vpad.css" />
<script type="text/javascript" src="/application/lib/datatables/js/jquery.dataTables.js"></script> 
<h1 class="page-title">Dietas</h1>
<div class="container_12 clearfix leading">
	<section class="portlet grid_12 leading">
		<header>
			<h2>Paciente</h2>
		</header>
		<section>
			<select name="patient">
				<?php
				foreach ($all_p as $p){
					echo '<option value="'.$p['id'].'">'.$p['name'].'</option>';
				}
				?>
			</select>
		</section>
	</section>
	<section class="portlet grid_12 leading">
		<header>
			<h2>Alimento</h2>
		</header>
		<section>
			<input type="text" id="amount" name="amount" pattern="[0-9]*" placeholder="Quantidade" />
			<input type="text" id="name_measure" readonly="readonly" name="name_measure" placeholder="Medida" />
			<select id="slct_food" name="food">
				<?php
				foreach ($all_f as $f){
					echo '<option value="'.$f['id'].'">'.$f['name'].'</option>';
				}
				?>
			</select>
			<select id="slct_time">
			<?php
				foreach ($all_t as $t){
						echo '<option value="'.$t['id'].'">'.$t['name'].'</option>';
					}
				?>
			</select>
			<input class="button" type="button" value="Salvar" id="btn_save_food"/>
			</section>
	</section>
	
	<section class="portlet grid_12 leading">
		<header>
			<h2>Café da Manhã</h2>
		</header>
		<section>
			<ul id="ul1">

			</ul>
		</section>
	</section>
	<section class="portlet grid_12 leading">
		<header>
			<h2>Colação</h2>
		</header>
		<section>
			<ul id="ul2">

			</ul>
		</section>
	</section>
	<section class="portlet grid_12 leading">
		<header>
			<h2>Almoço</h2>
		</header>
		<section>
			<ul id="ul3">

			</ul>
		</section>
	</section>
	<section class="portlet grid_12 leading">
		<header>
			<h2>Lanche</h2>
		</header>
		<section>
			<ul id="ul4">

			</ul>
		</section>
	</section>
	<section class="portlet grid_12 leading">
		<header>
			<h2>Jantar</h2>
		</header>
		<section>
			<ul id="ul5">
				
			</ul>
		</section>
	</section>
	<section class="portlet grid_12 leading">
		<header>
			<h2>Ceia</h2>
		</header>
		<section>
			<ul id="ul6">

			</ul>
		</section>
	</section>
	<section class="portlet grid_12 leading">
		<header>
			<h2>Informações nutricionais</h2>
		</header>
		<section style="padding: 2px">
			<table class="display"  id="tbl_diet" border="1">
				<thead>
					<tr>
						<th>kcal</th>
						<th>Cbo</th>
						<th>Ptn</th>
						<th>G. Tot</th>
						<th>G. Sat</th>
						<th>G. Trans</th>
						<th>Fbr</th>
						<th>Sódio</th>
					</tr>
				</thead>
				<tbody>
					<td class="center" id="total_kcal"> 0 </td>
					<td class="center" id="total_carb"> 0 </td>
					<td class="center" id="total_protein"> 0 </td>
					<td class="center" id="total_fat_t"> 0 </td>
					<td class="center" id="total_fat_s"> 0 </td>
					<td class="center" id="total_fat_tr"> 0 </td>
					<td class="center" id="total_fiber"> 0 </td>
					<td class="center" id="total_sodium"> 0 </td>
				</tbody>
			</table>
		</section>
	</section>
	<section class="portlet grid_12 leading">
		<header>
			<h2>Enviar Dieta</h2>
		</header>
		<section>
			<div class="button submit">Salvar</div>
		</section>
	</section>
</div>
<script>
	$(document).ready( function() {
		id_food_li = 0;
		r_id_food_li = 0;
		key_food_li = 0;
		
		diet = {
			"id_patient":$("select[name$='patient']").val(),
			"food_el": []
		};
		
		food = {};
		remove_food = {};
		
		total_kcal = 0;
		total_carb = 0;
		total_protein = 0;
		total_fat_t = 0;
		total_fat_s = 0;
		total_fat_tr = 0;
		total_fiber = 0;
		total_sodium = 0;
		
		$('#tbl_diet').dataTable( {
			"oLanguage": {
				"sProcessing":   "Processando...",
				"sLengthMenu":   "Mostrar _MENU_ registros",
				"sZeroRecords":  "Não foram encontrados resultados",
				"sInfo":         "Mostrando de _START_ até _END_ de _TOTAL_ registros",
				"sInfoEmpty":    "Mostrando de 0 até 0 de 0 registros",
				"sInfoFiltered": "(filtrado de _MAX_ registros no total)",
				"sInfoPostFix":  "",
				"sSearch":       "Buscar:",
				"sUrl":          "",
				"oPaginate": {
					"sFirst":    "Primeiro",
					"sPrevious": "Anterior",
					"sNext":     "Seguinte",
					"sLast":     "Último"
				}
			},
			"bRetrieve": true,
			"bPaginate": false,
			"bLengthChange": false,
			"bFilter": false,
			"bSort": false,
			"bInfo": false,
			"bAutoWidth": false
		});

		$(".dataTables_wrapper").css("min-height",0);


		$('#btn_save_food').click(function(){
			var amount		= $("#amount").val();
			var id_time		= $("#slct_time").val();
			addFood(id_time,amount);
		});

		$('.button.submit').click(function(){
			$.post('/ajax/new_diet',diet,function(r){
				alert('Dieta salva com sucesso');
				location.hash = "#pacientes";
			});
		});


		$('#slct_food').change(function(){
			var id_food = $(this).val();
			getFood(id_food,function(r){
				food = r;
				var measure;
				if (food.id_measure != 1){
					measure	= food.m_name_b;
				}else{
					measure = food.gml + " (g/ml)";
				}
				$('#name_measure').val(measure);
			});
		});
		
		$('.btn_remove_food').live('click',function(e){
			var food_li = {};
			r_id_food_li = parseInt($(this).parent().attr('id').substr(8));
			
			//pega o food_li
			 $.each(diet.food_el, function(key, val) {
				if(val != undefined){
					if(r_id_food_li == val.id_food_li){
						key_food_li = key;
						food_li = val;
					};
				}
			});
						
			//Pega o food do sistema
			var id_food = food_li.id_food;
			getFood(id_food, function(r){
				remove_food = r;
				removeFood(food_li);
			});
		});
		
		//Tem que adicionar a variavel e à view e a tabela
		function addFood(id_time,amount){
			//Adicionado na view
			id_ul		= "#ul"+id_time;
			
			if(food.id_measure != 1){
				$(id_ul).append("<li id='food_li_"+id_food_li+"' style='display:none'>"+amount+" "+food.m_name_b+" de "+food.name+"<span style='color: #999999; margin-left: 20px; cursor: pointer;' class='btn_remove_food'>Remover</span></li> ");
				$("#food_li_"+id_food_li).fadeIn();
			}else{
				var qtd = amount * food.gml;
				$(id_ul).append("<li id='food_li_"+id_food_li+"' style='display:none'>"+qtd+" (g/ml) de "+food.name+"<span style='color: #999999; margin-left: 20px; cursor: pointer;' class='btn_remove_food'>Remover</span></li> ");
				$("#food_li_"+id_food_li).fadeIn();			
			}
			
			
			//Adicionado na variavel
			diet.food_el.push({ "amount":amount, "id_food":food.id,"id_time":id_time,"id_food_li":id_food_li } );

			id_food_li	+= 1;
			//Adicionado na tabela
			total_kcal += amount * food.kcal;
			$('#total_kcal').html(total_kcal);
			
			total_carb += amount * food.carb;
			$('#total_carb').html(total_carb);
			
			total_protein += amount * food.protein;
			$('#total_protein').html(total_protein);
			
			total_fat_t += amount * food.fat_t;
			$('#total_fat_t').html(total_fat_t);
			
			total_fat_s += amount * food.fat_s;
			$('#total_fat_s').html(total_fat_s);
			
			total_fat_tr += amount * food.fat_tr;
			$('#total_fat_tr').html(total_fat_tr);
			
			total_fiber += amount * food.fiber;
			$('#total_fiber').html(total_fiber);
			
			total_sodium += amount * food.sodium;
			$('#total_sodium').html(total_sodium);
		}
		
		//Remove da view da tabela e da variavel
		function removeFood(food_li){
			$('#food_li_'+food_li.id_food_li).fadeOut();
			amount = food_li.amount;
			
			//remover da variavel
			diet.food_el.splice(key_food_li,1);
			
			
			//Remover da tabela
			total_kcal -= amount * remove_food.kcal;
			$('#total_kcal').html(total_kcal);
			
			total_carb -= amount * remove_food.carb;
			$('#total_carb').html(total_carb);
			
			total_protein -= amount * remove_food.protein;
			$('#total_protein').html(total_protein);
			
			total_fat_t -= amount * remove_food.fat_t;
			$('#total_fat_t').html(total_fat_t);
			
			total_fat_s -= amount * remove_food.fat_s;
			$('#total_fat_s').html(total_fat_s);
			
			total_fat_tr -= amount * remove_food.fat_tr;
			$('#total_fat_tr').html(total_fat_tr);
			
			total_fiber -= amount * remove_food.fiber;
			$('#total_fiber').html(total_fiber);
			
			total_sodium -= amount * remove_food.sodium;
			$('#total_sodium').html(total_sodium);
		}
		
		function getFood(id_food,callback){
			data = {
				id_food:id_food
			}
			$.post('/ajax/get_food',data, callback,'json');
		}
	});
</script>